package com.example.mymusicplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.browse.MediaBrowser;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;

import java.util.Timer;
import java.util.TimerTask;

public class MusicService extends Service {
    private MediaPlayer mediaPlayer;
    private Timer timer;

    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 实例化mediaPlayer
        mediaPlayer = new MediaPlayer();
    }


    //添加定时器，用于给主线程的播放器进度条设置时间
    public void addTimer(){
        if (timer == null){
            //实例化计时器
            timer = new Timer();
            //开启计时器线程任务
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    // 需要处理的逻辑
                    if(mediaPlayer == null) return;
                    int duration = mediaPlayer.getDuration();
                    int progress = mediaPlayer.getCurrentPosition();
                    // 创建消息对象，给主线程发送消息
                    Message msg = new Message();
                    // 将音乐的时间数据封装到bundle对象，给msg设置数据
                    Bundle bundle = new Bundle();
                    bundle.putInt("duration",duration);
                    bundle.putInt("progress",progress);
                    msg.setData(bundle);
                    //将消息发送到主线程的消息队列
                    MainActivity.handler.sendMessage(msg);
                }
            };
            // 开始计时任务后的5ms，第一次执行task，以后的每500ms执行一次
            timer.schedule(timerTask,5,500);
        }
    }
    // 内部类
    class MusicControl extends Binder {
        //播放音频
        public void play(){
            try {
                mediaPlayer.reset(); // 重置音频
                // 加载多媒体音乐文件
                mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.music);
                mediaPlayer.start(); //开始播放音频
                //自定义方法：添加定时器
                addTimer();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //暂停播放
        public void pausePlay(){
            if (mediaPlayer.isPlaying())
                mediaPlayer.pause();
        }
        //继续播放
        public void continuePlay(){
            if (!mediaPlayer.isPlaying())
                mediaPlayer.start();
        }
        //设置音乐播放位置
        public void seekTo(int progress){
            mediaPlayer.seekTo(progress);
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return new MusicControl();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mediaPlayer == null) return;
        mediaPlayer.stop(); //停止播放音频
        mediaPlayer.release(); //释放资源
        mediaPlayer = null;
    }
}